using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Autobot
{
    class LogController
    {
        StreamWriter log_file_writer = null;

        public bool OpenLog(String log_file_name)
        {
            bool r;

            lock (this)
            {
                if (log_file_writer == null)
                {
                    //if arg2 is true, append mode is on, otherwise the file is cleared if it exists
                    log_file_writer = new StreamWriter(log_file_name, false);
                    r = true;
                }
                else
                {
                    r = false;
                }
            }

            if (r) { WriteLog("--- Log Start ---"); }

            return r;
        }

        public bool WriteLog(String s)
        {
            bool r;

            lock (this)
            {
                if (log_file_writer != null)
                {
                    DateTime dt = DateTime.Now; // obtain current time 
                    log_file_writer.WriteLine("{0:s}: " + s, dt);
                    log_file_writer.Flush();
                    r = true;
                }
                else
                {
                    r = false;
                }
            }

            return r;
        }

        public void CloseLog()
        {
            lock (this)
            {
                if (log_file_writer != null)
                {
                    WriteLog("--- Log End ---");
                    log_file_writer.Close();
                    log_file_writer = null;
                }

            }
        }

    }
}
